package com.briup.jd2511.jd2511_product_source.service.impl;

import cn.hutool.core.util.IdUtil;
import com.briup.jd2511.jd2511_product_source.dao.DiseaseRecordMapper;
import com.briup.jd2511.jd2511_product_source.dao.ManagerAnimalMapper;
import com.briup.jd2511.jd2511_product_source.dao.ext.DiseaseRecordExtMapper;
import com.briup.jd2511.jd2511_product_source.exception.ServiceException;
import com.briup.jd2511.jd2511_product_source.pojo.DiseaseRecord;
import com.briup.jd2511.jd2511_product_source.pojo.ManagerAnimal;
import com.briup.jd2511.jd2511_product_source.pojo.ManagerDisease;
import com.briup.jd2511.jd2511_product_source.result.Result;
import com.briup.jd2511.jd2511_product_source.result.ResultCode;
import com.briup.jd2511.jd2511_product_source.service.DiseaseRecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class DiseaseRecordServiceImpl implements DiseaseRecordService {

    @Autowired
    private DiseaseRecordMapper diseaseRecordMapper;
    @Autowired
    private DiseaseRecordExtMapper diseaseRecordExtMapper;
    @Autowired
    private ManagerAnimalMapper animalMapper;


    /**
     * 查询所有病症
     *
     * @return
     */
    @Override
    public List<ManagerDisease> findAllDisease() {
        return diseaseRecordMapper.selectAllDiseases();
    }

    /**
     * 分页+条件查询病症记录
     *
     * @param pageName
     * @param pageSize
     * @param drStatus
     * @param drDId
     * @return
     */
    @Override
    public PageInfo<ManagerDisease> findByPage(Integer pageName, Integer pageSize, String drStatus, String drDId) throws ServiceException {
        // 开启分页
        PageHelper.startPage(pageName, pageSize);
        // 条件查询
        List<ManagerDisease> list = diseaseRecordExtMapper.selectDiseasedAnimal(drStatus, drDId);
        return new PageInfo<>(list);
    }

    /**
     * 添加/修改病症记录
     *
     * @param diseaseRecord
     * @return
     */
    @Override
    public void saveOrUpdate(DiseaseRecord diseaseRecord) throws ServiceException{
        //此接口用于新增或修改病症治疗记录信息，请求参数中包含治疗记录ID则修改，不包含则为新增。
        //无论新增修改，动物编号、病症描述不能为空
        //只有养殖中的动物能够添加病症记录
        //添加时未指定诊疗状态，默认为未治疗
        //如果添加或修改的治疗记录信息中，相应动物诊疗状态为未治疗或治疗中，则需同步修改动物健康状态为生病；如果诊疗状态为已治疗，则需同步修改动物的健康状态为健康
        if(!StringUtils.hasText(diseaseRecord.getDrAnimalId())||!StringUtils.hasText(diseaseRecord.getDrDesc())){
            //动物编号、病症描述不能为空
            throw new ServiceException(ResultCode.PARAM_IS_EMPTY);
        }
        //动物校验
        String animalId = diseaseRecord.getDrAnimalId();
        ManagerAnimal animalFromDB = animalMapper.selectById(animalId);
        if(animalFromDB==null){
            //不存在此动物编号
            throw new ServiceException(ResultCode.DATA_IS_EMPTY);
        } else if(!animalFromDB.getAStatus().equals("养殖中")){
            //此动物不是养殖中的动物
            throw new ServiceException(ResultCode.DATA_IS_EMPTY);
        }
        int result = 0;
        Integer drId = diseaseRecord.getDrId();
        String drStatus = diseaseRecord.getDrStatus();
        if (drId != null) {
            //修改
            result = diseaseRecordMapper.updateSelective(diseaseRecord);
        }
        else{
            //添加
            //未传诊疗状态，默认为未治疗
            if(drStatus==null){
                diseaseRecord.setDrStatus("未治疗");
                result = diseaseRecordMapper.insert(diseaseRecord);
            }
        }
        // 添加病症记录失败
        if(result == 0){
            throw new ServiceException(ResultCode.FAIL);
        }
        //添加病症记录成功，根据诊疗状态修改动物健康状态
        if(drStatus.equals("未治疗")||drStatus.equals("治疗中")){
            animalFromDB.setAHealthy("生病");
            animalMapper.updateSelective(animalFromDB);
        }
        else{
            animalFromDB.setAHealthy("健康");
            animalMapper.updateSelective(animalFromDB);
        }
    }
}
